package com.app.controller;


import com.app.common.*;
import com.app.model.FieldSchoolModel;
import com.app.model.IndustryModel;
import com.app.model.UpperSchoolModel;
import com.app.service.FieldSchoolService;
import com.app.service.IndustryService;
import com.app.service.UpperSchoolService;
import com.app.util.Result;
import com.app.vo.ExperienceVo;
import com.app.vo.NatureVo;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;


@RestController
@Controller
@NoArgsConstructor
@Data
public class FinancingController {

    @Autowired
    UpperSchoolService upperSchoolService;
    @Autowired
    FieldSchoolService fieldSchoolService;
    @Autowired
    IndustryService industryService;

    /**
     * 查询融资阶段
     */
    @GetMapping("/getFinancing")//后台查融资阶段
    public Result<Object> getFinancing(HttpServletRequest request){

        return Result.success(FinancingTool.FINANCING_VO_LIST);
    }


    /**
     * 查询规模
     */
    @GetMapping("/getScale")
    public Result<Object> getScale(HttpServletRequest request){

        return Result.success(ScaleTool.SCALE_VO_LIST);
    }

    /**
     * 查询工作经验
     */
    @GetMapping("/getExperience")
    public Result<Object> getExperience(HttpServletRequest request){
        return Result.success(ExperienceTool.EXPERIENCE_VO_LIST);
    }

    /**
     * 查询学历
     */
    @GetMapping("/getEducation")
    public Result<Object> getEducation(HttpServletRequest request){
        return Result.success(EducationTool.EDUCATION_VO_LIST);
    }

    /**
     * 查询行业
     */
    @GetMapping("/getIndustryList")
    public Result<Object> getIndustryList(HttpServletRequest request){

        List<ExperienceVo> industryList = new ArrayList<>();
        for (int i = 0; i <= IndustryTool.myMap.size(); i++) {
            if (IndustryTool.myMap.get(i) != null) {
                ExperienceVo vo = new ExperienceVo();
                vo.setId(i);
                vo.setData(IndustryTool.myMap.get(i));
                industryList.add(vo);
            }
        }

        return Result.success(industryList);
    }

    /**
     * 查询行业
     */
    @GetMapping("/getNatureList")
    public Result<Object> getNatureList() {
        List<NatureVo> natureList = new ArrayList<>();
        for (int i = 0; i <= NatureTool.myMap.size(); i++) {
            if (NatureTool.myMap.get(i) != null) {
                NatureVo vo = new NatureVo();
                vo.setId(i);
                vo.setName(NatureTool.myMap.get(i));
                natureList.add(vo);
            }
        }
        return Result.success(natureList);
    }
    /**
     * 查询全部高校
     */
    @GetMapping("/getSchool")
    public Result<Object> getSchool(@RequestParam("keyword") String keyword) {
        List<UpperSchoolModel> modelList = upperSchoolService.getSchool(keyword);
        return Result.success(modelList);
    }

    /**
     * 查询全部高校
     */
    @GetMapping("/getSchoolAll")
    public Result<Object> getSchoolAll() {
        List<UpperSchoolModel> modelList = upperSchoolService.getSchoolAll();
        return Result.success(modelList);
    }

    /**
     * 查询全部专业
     */
    @GetMapping("/getField")
    public Result<Object> getField(@RequestParam("keyword") String keyword) {
        List<FieldSchoolModel> modelList = fieldSchoolService.getListByKeyWord(keyword);
        return Result.success(modelList);
    }

    /**
     * 查询全部专业
     */
    @GetMapping("/getFieldAll")
    public Result<Object> getFieldAll() {
        List<FieldSchoolModel> modelList = fieldSchoolService.getAll();
        return Result.success(modelList);
    }
//    private List<FieldSchoolModel> findChildren(List<FieldSchoolModel> modelList, String fatherCode){
//        List<FieldSchoolModel> personModelList= new ArrayList<>();
//        for (int i = 0; i < modelList.size(); i++) {
//            if(modelList.get(i).getFatherCode().equals(fatherCode))
//            {
//                FieldSchoolModel model = modelList.get(i);
//                List<FieldSchoolModel> modelList1 = findChildren(modelList,model.getFieldCode());
//                for (int j = 0; j < modelList1.size(); j++) {
//                    if(modelList1!=null&&modelList1.size()>0)
//                    {
//                        model.setSonList(modelList1);
//                    }
//                }
//                personModelList.add(model);
//            }
//        }
//        return personModelList;
//    }


    /**
     * 查询全部专业
     */
    @GetMapping("/getIndustryAll")
    public Result<Object> getIndustryAll() {
        List<IndustryModel> modelList = industryService.getAll();
        return Result.success(findChildren2(modelList,"0"));
    }

    private List<IndustryModel> findChildren2(List<IndustryModel> modelList, String fatherCode){
        List<IndustryModel> personModelList= new ArrayList<>();
        for (int i = 0; i < modelList.size(); i++) {
            if(modelList.get(i).getParentCode().equals(fatherCode))
            {
                IndustryModel model = modelList.get(i);
                List<IndustryModel> modelList1 = findChildren2(modelList,model.getIndustryCode());
                for (int j = 0; j < modelList1.size(); j++) {
                    if(modelList1!=null&&modelList1.size()>0)
                    {
                        model.setSonList(modelList1);
                    }
                }
                personModelList.add(model);
            }
        }
        return personModelList;
    }
}